package com.zhihuishu.micro.course.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import redis.clients.jedis.Jedis;

import com.zhihuishu.micro.course.dto.ChapterDto;
import com.zhihuishu.micro.course.helper.LogModule;
import com.zhihuishu.micro.course.helper.RemoteLogHelper;
import com.zhihuishu.micro.course.service.ChapterTreenityDBService;
import com.zhihuishu.micro.course.service.ChapterTreenityRedisService;
import com.zhihuishu.micro.course.service.ChapterTreenityService;
import com.zhihuishu.micro.course.service.TreenityBatchDBService;
import com.zhihuishu.micro.course.service.TreenityBatchRedisService;
import com.zhihuishu.toolkit.jedis.JedisHelper;
import com.zhihuishu.toolkit.jedis.template.JedisExecutor;

@Service("chapterAllService")
public class ChapterAllServiceImpl extends BaseTreenityBatchServiceImpl<ChapterDto> implements ChapterTreenityService {

    @Resource
    private ChapterTreenityDBService chapterAllServiceDB;

    @Resource
    private ChapterTreenityRedisService chapterAllServiceRedis;

    @Resource
    private JedisExecutor jedisExecutor;

    @Override
    protected TreenityBatchDBService<ChapterDto, ?> getDBService() {
        return chapterAllServiceDB;
    }

    @Override
    protected Class<ChapterDto> getDTOClass() {
        return ChapterDto.class;
    }

    @Override
    protected TreenityBatchRedisService<ChapterDto> getRedisService() {
        return chapterAllServiceRedis;
    }

    @Override
    public void update(ChapterDto chapterDto, long updateUserId) {
        super.update(chapterDto, updateUserId);

        // 维护课程-完整章关系
        // "cc:course", courseId, "chapters" 完整章
        // "cc:course", courseId, "chapters:all" 包含不完整章
        Integer isPass = chapterDto.getIsPass();
        final Integer chapterId = chapterDto.getId();
        if (isPass != null) {
            Long courseId = queryCourseId(chapterId);
            chapterDto.setCourseId(courseId);

            log.warn("更新了isPass:[{}]，同步更新课程-完整章关系缓存[{}]!", isPass, courseId);
            final String chapterAllRelationKey = JedisHelper.key("cc:course", courseId, "chapters");
            if (isPass == 1) {
                jedisExecutor.execute(new JedisHelper.Handler() {
                    @Override
                    public void handle(Jedis jedis) {
                        jedis.sadd(chapterAllRelationKey, String.valueOf(chapterId));

                        log.warn("添加了完整章[{}]，当前课程下完整章包括:[{}]!", chapterId,
                                jedis.smembers(chapterAllRelationKey));
                    }
                });
            } else {
                jedisExecutor.execute(new JedisHelper.Handler() {
                    @Override
                    public void handle(Jedis jedis) {
                        jedis.srem(chapterAllRelationKey, String.valueOf(chapterId));
                        log.warn("删除了不完整章[{}]，当前课程下完整章包括:[{}]!", chapterId,
                                jedis.smembers(chapterAllRelationKey));
                    }
                });
            }
        }

    }

    private Long queryCourseId(Integer chapterId) {
        final ChapterDto detail = detail(chapterId);
        return detail.getCourseId();
    }

    @Override
    public Integer create(ChapterDto chapter, long creator) {
        Assert.notNull(chapter, "章对象为空!");
        Assert.notNull(chapter.getCourseId(), "章所属课程ID为空!");
        Assert.notNull(chapter.getRank(), "章序号为空!");
        RemoteLogHelper.log(LogModule.CHAPTER_SAVE.getCode(), creator, "[开始]创建章[课程ID：{}/序号：{}]",
                chapter.getCourseId(), chapter.getRank());

        Integer chapterId = super.create(chapter, creator);

        RemoteLogHelper.log(LogModule.CHAPTER_SAVE.getCode(), creator, "[完成]创建章[课程ID：{}/序号：{}]({})",
                chapter.getCourseId(), chapter.getRank(), chapterId);
        return chapterId;
    }

}
